package com.easy.common.util.form;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.easy.common.util.ReflectUtility;
import com.easy.common.util.RegexpUtility;
import com.easy.common.util.Utility;

public class FOM
{

	public FOM()
	{
	}

	public static Object build(Map request)
		throws Exception
	{
		Object paramObj = request.get("dtoClass");
		if(paramObj==null)return null;
		String mainClassName = null;
		if (paramObj instanceof String)
			mainClassName = (String)paramObj;
		else
			mainClassName = ((String[])paramObj)[0];
		if (Utility.isNullorEmpty(mainClassName)){
			return null;
		}
		else {
			Class mainClass = Class.forName(mainClassName);
			return build(request, mainClass, null);
		}
	}

	public static Object build(Map request, Class cls, String postFix)
		throws Exception
	{
		Object obj = cls.newInstance();
		Field fieldArray[] = cls.getDeclaredFields();
		for (int i = 0; i < fieldArray.length; i++)
		{
			Field field = fieldArray[i];
			Class fieldType = field.getType();
			String fieldName = field.getName();
			if ((List.class.isAssignableFrom(fieldType)) || (Map.class.isAssignableFrom(fieldType))){
				String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName.substring(0, fieldName.length() - 3);
				Object paramObj = request.get((new StringBuilder(String.valueOf(tmpStr))).append("Class").toString());
				String className = null;
				String detlCntStr = null;
				if (paramObj != null)
				{
					if (paramObj instanceof String)
						className = (String)paramObj;
					else
						className = ((String[])paramObj)[0];
					paramObj = request.get((new StringBuilder(String.valueOf(tmpStr))).append(Utility.isNullorEmpty(postFix) ? "" : (new StringBuilder("_")).append(postFix).toString()).append("Cnt").toString());
					if (paramObj instanceof String)
						detlCntStr = (String)paramObj;
					else
						detlCntStr = ((String[])paramObj)[0];
				}
				if (!Utility.isNullorEmpty(className) && !Utility.isNullorEmpty(detlCntStr))
				{
					Class detlClass = Class.forName(className);
					int detlCnt = Integer.parseInt(detlCntStr);
					for (int j = 0; j < detlCnt; j++)
					{
						String detlPostFix = null;
						if (Utility.isNullorEmpty(postFix))
							detlPostFix = (new StringBuilder(String.valueOf(tmpStr))).append("_").append(String.valueOf(j)).toString();
						else
							detlPostFix = (new StringBuilder(String.valueOf(postFix))).append("_").append(tmpStr).append("_").append(j).toString();
						Object detlObj = build(request, detlClass, detlPostFix);
						ReflectUtility.addObj(obj, detlObj, fieldName);
					}

				}
			}
			else
			{
				String paramName = fieldName;
				if (!Utility.isNullorEmpty(postFix))
					paramName = (new StringBuilder(String.valueOf(paramName))).append("_").append(postFix).toString();
				Object paramObj = request.get(paramName);
				if (paramObj != null)
				{
					String paramValue = null;
					if (paramObj instanceof String)
						paramValue = (String)paramObj;
					else
						paramValue = ((String[])paramObj)[0];
					ReflectUtility.setValue(obj, fieldName, field.getType(), paramValue);
				}
			}
		}
		return obj;
	}

	public static Map buildMap(Map request)
		throws Exception
	{
		Map resualt = new HashMap();
		Map subTableMap = getValue2Map(request, "detlTable_");
		Set keys = subTableMap.keySet();
		String subTableName;
		List subTable;
		for (Iterator iterator = keys.iterator(); iterator.hasNext(); resualt.put(subTableName, subTable))
		{
			String key = (String)iterator.next();
			String sp[] = key.split("_");
			String subIndex = sp[sp.length - 1];
			subTableName = ((String[])subTableMap.get(key))[0];
			request.remove(key);
			Map subFieldMap = getValue2Map(request, (new StringBuilder("_")).append(subIndex).append("_").toString());
			subTable = new ArrayList();
			int i = 0;
			String reg = (new StringBuilder("_")).append(subIndex).append("_").toString();
			for (Map subFieldMapf = getValue2Map(request, (new StringBuilder(String.valueOf(reg))).append(i).toString()); subFieldMapf.size() > 0; subFieldMapf = getValue2Map(request, (new StringBuilder(String.valueOf(reg))).append(i).toString()))
			{
				Map sub = new HashMap();
				Set subKeys = subFieldMapf.keySet();
				String subKey;
				for (Iterator iterator2 = subKeys.iterator(); iterator2.hasNext(); request.remove(subKey))
				{
					subKey = (String)iterator2.next();
					String valueTemp = subKey.split("_")[0];
					String value = ((String[])subFieldMapf.get(subKey))[0];
					sub.put(valueTemp, value);
				}

				subTable.add(sub);
				i++;
			}

		}

		keys = request.keySet();
		String key;
		String fieldValue;
		for (Iterator iterator1 = keys.iterator(); iterator1.hasNext(); resualt.put(key, fieldValue))
		{
			key = (String)iterator1.next();
			fieldValue = ((String[])request.get(key))[0];
		}

		return resualt;
	}

	private static Map getValue2Map(Map m, String reg)
	{
		Map resualt = new HashMap();
		Set keys = m.keySet();
		for (Iterator iterator = keys.iterator(); iterator.hasNext();)
		{
			String key = (String)iterator.next();
			if (key.contains(reg))
			{
				Object value = m.get(key);
				resualt.put(key, value);
			}
		}

		return resualt;
	}

	public static List buildList(Map request)
		throws Exception
	{
		List resualt = new ArrayList();
		String mainTable = ((String[])request.get("mainTable"))[0];
		resualt.add(mainTable);
		Map subTableMap = getValue2Map(request, "detlTable_");
		Set keys = subTableMap.keySet();
		List sub = new ArrayList();
		String subTableName;
		for (Iterator iterator = keys.iterator(); iterator.hasNext(); sub.add(subTableName))
		{
			String key = (String)iterator.next();
			subTableName = ((String[])subTableMap.get(key))[0];
		}

		resualt.add(sub);
		return resualt;
	}

	public static boolean isCollect(Class interfaceArray[])
	{
		for (int i = 0; i < interfaceArray.length; i++)
		{
			Class cls = interfaceArray[i];
			if (cls.equals(java.util.Collection.class) || cls.equals(Map.class))
				return true;
		}

		return false;
	}

	public static StringBuffer toJson(Object obj)
		throws Exception
	{
		if (obj == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		Class cls = obj.getClass();
		Field fieldArray[] = cls.getDeclaredFields();
		int length = fieldArray.length;
		for (int i = 0; i < length; i++)
		{
			Field field = fieldArray[i];
			Class fieldType = field.getType();
			String fieldName = field.getName();
			//System.out.println("fldname="+fieldName);
			rtBuf.append((new StringBuilder("\"")).append(fieldName).append("\"").toString());
			rtBuf.append(":");
			Object value = null;

			if (List.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))
			{
				//String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName.substring(0, fieldName.length() - 3);
				
				//String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName;
				StringBuffer buf = new StringBuffer();
				buf.append("[");
				/*
				Iterator iObj = MYReflectUtility.itObject(obj, tmpStr);
				int cntFlag = 0;
				Object currObj;
				while(iObj.hasNext()){
					currObj=iObj.next();
					if(cntFlag>0)buf.append(",");
					buf.append(currObj);
					cntFlag++;
				}
*/
				try{
					List list=(List) ReflectUtility.getValue(obj, fieldName);
					if(list!=null){
						for(int ki=0;ki<list.size();ki++){
							Object obj1=(Object)list.get(ki);
							if(ki>0)buf.append(",");
							buf.append(toJson(obj1));
						}
					}
				}catch(Exception ex){}
				buf.append("]");
				value = buf;
			} else
			if (Integer.TYPE.equals(fieldType) || Integer.class.equals(fieldType) || Double.TYPE.equals(fieldType) || Double.class.equals(fieldType))
				value = ReflectUtility.getValue(obj, fieldName);
			else
			if (Date.class.equals(fieldType))
			{
				if ((Date)ReflectUtility.getValue(obj, fieldName) != null)
					value = (new StringBuilder("\"")).append(Utility.getDateTimeStr((Date)ReflectUtility.getValue(obj, fieldName))).append("\"").toString();
				else
					value = "\"\"";
			} else
			{
				Object valueObj = ReflectUtility.getValue(obj, fieldName);
				if (valueObj == null)
				{
					value = "\"\"";
				} else
				{
					String tmpStr = Utility.null2Empty(valueObj.toString());
					tmpStr = encodeSpecial(tmpStr);
					value = (new StringBuilder("\"")).append(tmpStr).append("\"").toString();
				}
			}
			rtBuf.append(value);
			if (i < length - 1)
				rtBuf.append(",");
		}

		rtBuf.append("}");
		return rtBuf;
	}
	public static Map toMap(Object obj)
	throws Exception
	{
		if (obj == null)return new HashMap();
		Map map=new HashMap();
		Class cls = obj.getClass();
		Field fieldArray[] = cls.getDeclaredFields();
		int length = fieldArray.length;
		for (int i = 0; i < length; i++)
		{
			Field field = fieldArray[i];
			String fieldName = field.getName();
			//System.out.println(fieldName+"=="+MYReflectUtility.getValue(obj, fieldName));
			map.put(fieldName, ReflectUtility.getValue(obj, fieldName));
		}
		return map;
	}

	public static StringBuffer toJson2(Object obj)
		throws Exception
	{
		if (obj == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer();
		Class cls = obj.getClass();
		Field fieldArray[] = cls.getDeclaredFields();
		int length = fieldArray.length;
		for (int i = 0; i < length; i++)
		{
			Field field = fieldArray[i];
			Class fieldType = field.getType();
			String fieldName = field.getName();
			Object value = null;
			if (List.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))
			{
				String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName.substring(0, fieldName.length() - 3);
				StringBuffer buf = new StringBuffer();
				buf.append("[");
				Iterator iObj = ReflectUtility.itObject(obj, tmpStr);
				int cntFlag = 0;
				Object currObj;
				for (; iObj.hasNext(); buf.append(toJson(currObj)))
				{
					currObj = iObj.next();
					if (cntFlag > 0)
						buf.append(",");
					cntFlag++;
				}

				buf.append("]");
				value = buf;
			} else
			if (Integer.TYPE.equals(fieldType) || Integer.class.equals(fieldType) || Double.TYPE.equals(fieldType) || Double.class.equals(fieldType))
				value = (new StringBuilder("\"")).append(ReflectUtility.getValue(obj, fieldName)).append("\"").toString();
			else
			if (Date.class.equals(fieldType))
			{
				if ((Date)ReflectUtility.getValue(obj, fieldName) != null)
					value = (new StringBuilder("\"")).append(Utility.getDateTimeStr((Date)ReflectUtility.getValue(obj, fieldName))).append("\"").toString();
				else
					value = "\"\"";
			} else
			{
				Object valueObj = ReflectUtility.getValue(obj, fieldName);
				if (valueObj == null)
				{
					value = "\"\"";
				} else
				{
					String tmpStr = Utility.null2Empty(valueObj.toString());
					tmpStr = encodeSpecial(tmpStr);
					value = (new StringBuilder("\"")).append(tmpStr).append("\"").toString();
				}
			}
			rtBuf.append(value);
			if (i < length - 1)
				rtBuf.append(",");
		}

		return rtBuf;
	}

	public static StringBuffer toJson(Map map)
		throws Exception
	{
		if (map == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		String value;
		/*
		for (Iterator iKeys = map.keySet().iterator(); iKeys.hasNext(); rtBuf.append((new StringBuilder("\"")).append(value).append("\",").toString()))
		{
			String key = (String)iKeys.next();
			value = (String)map.get(key);
			if (value == null)
				value = "";
			rtBuf.append(key);
			rtBuf.append(":");
		}*/
		Iterator iKeys = map.entrySet().iterator();
		while(iKeys.hasNext())
		{
			
			Map.Entry entry = (Map.Entry) iKeys.next();
			String key = (String)entry.getKey();
			value="";
			try{
				value = VauleToStr(entry.getValue());
			}catch(Exception ex){}
			if (value == null)value = "";
			//rtBuf.append(key);
			rtBuf.append((new StringBuilder("\"")).append(key).append("\"").toString());
			rtBuf.append(":");
			rtBuf.append((new StringBuilder("\"")).append(value).append("\",").toString());
		}

		if (rtBuf.length() > 2)
			rtBuf.delete(rtBuf.length() - 1, rtBuf.length());
		rtBuf.append("}");
		return rtBuf;
	}
	public static StringBuffer toGridJson(Object obj)
	throws Exception
	{
		if (obj == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		Class cls = obj.getClass();
		Field fieldArray[] = cls.getDeclaredFields();
		int length = fieldArray.length;
		for (int i = 0; i < length; i++)
		{
			Field field = fieldArray[i];
			Class fieldType = field.getType();
			String fieldName = field.getName();
			Object value = null;
			if (List.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))
			{
				String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName.substring(0, fieldName.length() - 3);
				StringBuffer buf = new StringBuffer();
				buf.append("[");
				Iterator iObj = ReflectUtility.itObject(obj, tmpStr);
				int cntFlag = 0;
				Object currObj;
				for (; iObj.hasNext(); buf.append(toJson(currObj)))
				{
					currObj = iObj.next();
					if (cntFlag > 0)
						buf.append(",");
					cntFlag++;
				}
	
				buf.append("]");
				value = buf;
			} else
			if (Integer.TYPE.equals(fieldType) || Integer.class.equals(fieldType) || Double.TYPE.equals(fieldType) || Double.class.equals(fieldType))
				value = ReflectUtility.getValue(obj, fieldName);
			else
			if (Date.class.equals(fieldType))
			{
				if ((Date)ReflectUtility.getValue(obj, fieldName) != null)
					value = (new StringBuilder("\"")).append(Utility.getDateTimeStr((Date)ReflectUtility.getValue(obj, fieldName))).append("\"").toString();
				else
					value = "\"\"";
			} else
			{
				Object valueObj = ReflectUtility.getValue(obj, fieldName);
				if (valueObj == null)
				{
					value = "\"\"";
				} else
				{
					String tmpStr = Utility.null2Empty(valueObj.toString());
					tmpStr = encodeSpecial(tmpStr);
					value = (new StringBuilder("\"")).append(tmpStr).append("\"").toString();
				}
			}
			if(i==0){
				rtBuf.append("id:").append(value).append(",");
				rtBuf.append("data:[");
			}
			else {
				rtBuf.append(value).append(",");
			}
		}
		if (rtBuf.length() > 2){
			rtBuf.deleteCharAt(rtBuf.length()-1);
			rtBuf.append("]");
		}
		rtBuf.append("}");
		return rtBuf;
	}

	public static StringBuffer toGridJson(Map map)
	throws Exception
	{
		if (map == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		String value;
		int trc=0;
		Iterator iKeys = map.entrySet().iterator();
		while(iKeys.hasNext())
		{
			Map.Entry entry = (Map.Entry) iKeys.next();
			value="";
			try{
				value = VauleToStr(entry.getValue());
			}catch(Exception ex){}
			if (value == null)value = "";
			if(trc==0){
				rtBuf.append("\"id\":").append((new StringBuilder("\"")).append(value).append("\",").toString());
				rtBuf.append("\"data\":[");
			}
			else rtBuf.append((new StringBuilder("\"")).append(value).append("\",").toString());
			trc++;
		}
		if (rtBuf.length() > 2){
			rtBuf.deleteCharAt(rtBuf.length()-1);
			rtBuf.append("]");
		}
		rtBuf.append("}");
		return rtBuf;
	}
	public static StringBuffer toGridTreeJson(Object obj)
	throws Exception
	{
		if (obj == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		Class cls = obj.getClass();
		Field fieldArray[] = cls.getDeclaredFields();
		int length = fieldArray.length;
		for (int i = 0; i < length; i++)
		{
			Field field = fieldArray[i];
			Class fieldType = field.getType();
			String fieldName = field.getName();
			Object value = null;
			if (List.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))
			{
				String tmpStr = fieldName.endsWith("List") ? fieldName.substring(0, fieldName.length() - 4) : fieldName.substring(0, fieldName.length() - 3);
				StringBuffer buf = new StringBuffer();
				buf.append("[");
				Iterator iObj = ReflectUtility.itObject(obj, tmpStr);
				int cntFlag = 0;
				Object currObj;
				for (; iObj.hasNext(); buf.append(toJson(currObj)))
				{
					currObj = iObj.next();
					if (cntFlag > 0)
						buf.append(",");
					cntFlag++;
				}
	
				buf.append("]");
				value = buf;
			} else
			if (Integer.TYPE.equals(fieldType) || Integer.class.equals(fieldType) || Double.TYPE.equals(fieldType) || Double.class.equals(fieldType))
				value = ReflectUtility.getValue(obj, fieldName);
			else
			if (Date.class.equals(fieldType))
			{
				if ((Date)ReflectUtility.getValue(obj, fieldName) != null)
					value = (new StringBuilder("\"")).append(Utility.getDateTimeStr((Date)ReflectUtility.getValue(obj, fieldName))).append("\"").toString();
				else
					value = "\"\"";
			} else
			{
				Object valueObj = ReflectUtility.getValue(obj, fieldName);
				if (valueObj == null)
				{
					value = "\"\"";
				} else
				{
					String tmpStr = Utility.null2Empty(valueObj.toString());
					tmpStr = encodeSpecial(tmpStr);
					value = (new StringBuilder("\"")).append(tmpStr).append("\"").toString();
				}
			}
			if(i==0){
				rtBuf.append("id:").append(value).append(",");
				rtBuf.append("data:[");
			}
			else {
				rtBuf.append(value).append(",");
			}
		}
		if (rtBuf.length() > 2){
			rtBuf.deleteCharAt(rtBuf.length()-1);
			rtBuf.append("]");
		}
		rtBuf.append("}");
		return rtBuf;
	}

	public static StringBuffer toJson2(Map map)
	throws Exception
	{
		if (map == null)
			return new StringBuffer("{}");
		StringBuffer rtBuf = new StringBuffer("{");
		String value;
		Iterator iKeys = map.entrySet().iterator();
		while(iKeys.hasNext())
		{
			Map.Entry entry = (Map.Entry) iKeys.next();
			Object key = entry.getKey();
			value="";
			try{
				value = VauleToStr(entry.getValue());
			}catch(Exception ex){}
			if (value == null)value = "";
			rtBuf.append(key);
			rtBuf.append(":");
			rtBuf.append((new StringBuilder("\"")).append(value).append("\",").toString());
		}
	
		if (rtBuf.length() > 2)
			rtBuf.delete(rtBuf.length() - 1, rtBuf.length());
		rtBuf.append("}");
		return rtBuf;
	}

	public static Object json2Obj(String json, Class cls)
		throws Exception
	{
		Map propMap = json2Map(json);
		return build(propMap, cls, null);
	}

	public static Map json2Map(String json)
		throws Exception
	{
		Map propMap = new HashMap();
		List tmpArray = RegexpUtility.splitJson(json);
		Iterator iterator = tmpArray.iterator();
		while(iterator.hasNext())
		{
			String propStr = (String)iterator.next();
			propStr = propStr.trim();
			int tmpInt = propStr.indexOf(":");
			if (tmpInt >= 0)
			{
				String tmpArray2[] = new String[2];
				tmpArray2[0] = propStr.substring(0, tmpInt);
				tmpArray2[1] = propStr.substring(tmpInt + 1);
				if (tmpArray2.length >= 2)
				{
					tmpArray2[0] = tmpArray2[0].trim();
					tmpArray2[1] = tmpArray2[1].trim();
					if (tmpArray2[0].charAt(0) == '"' || tmpArray2[0].charAt(0) == '\'')
						tmpArray2[0] = tmpArray2[0].substring(1, tmpArray2[0].length() - 1);
					if (tmpArray2[1].charAt(0) == '"' || tmpArray2[1].charAt(0) == '\'')
						tmpArray2[1] = tmpArray2[1].substring(1, tmpArray2[1].length() - 1);
					propMap.put(tmpArray2[0], tmpArray2[1]);
				}
			}
		}
		return propMap;
	}
	public static Object RequestToClass(Object obj, HttpServletRequest request)
    {
        Class clazz = obj.getClass();
        try
        {
        if(clazz.getSimpleName().equals("Object")){  
            return clazz;  
        }  
        Field[] fields = clazz.getDeclaredFields();  
        if (fields == null || fields.length <= 0) {  
            throw new Exception("当前对象中没有任何属性值");  
        }  
        boolean isAccessible = false;
        for(int i=0;i<fields.length;i++){ 
            isAccessible = false;
            if(!fields[i].isAccessible())
            {
	            fields[i].setAccessible(true);  
	            isAccessible = true;
            }
            String name=fields[i].getName();
            Object value=fields[i].getType();
            String tval=request.getParameter(name);
            if(tval!=null)
            {
            	fields[i].set(obj, StrToVaule(value,tval));
            }
            if(isAccessible)fields[i].setAccessible(false);  
        }  
        }
        catch(Exception e){
            System.out.println("出错:" + e);
        }
        return obj;
    }
	// string 类型转化
	public static Object StrToVaule(Object value,String s)
	{
	//GetPublicMsg msg = new GetPublicMsg();
		if (value == null)return null;
		Class clazz = (Class)value;
		if (clazz == null)return null;
		Object o =null;

		try{
			if (s.length()<1 || s==null){
				if (clazz == int.class) {  
					return 0;  
				}  
				else if (clazz == long.class) {  
					return 0;  
				}  
				else if (clazz == java.math.BigDecimal.class) {  
					return null;  
				}  
				else if (clazz == double.class) {  
					return 0.0;  
				}  
				else if (clazz == byte.class) {  
					return 0;  
				}  
				else if (clazz == float.class) {  
					return 0.0;  
				}  
				else if (clazz == short.class) {  
					return 0;  
				}  
				else if (clazz == boolean.class) {  
					return false;  
				}  else if (clazz == String.class) {  
					return null;
				} else if (clazz == Date.class) {  
					return null;
 
				} else if (clazz == Timestamp.class) {  
					return null;
				} else if (clazz == java.sql.Time.class) {  
					return null;
				} else {  
					return null;
				}
			}
			else {
				if (clazz == int.class) {  
					return (Object)Integer.parseInt(s);  
				}  
				else if (clazz == long.class) {  
					return (Object)Long.parseLong(s);  
				}  
				else if (clazz == double.class) {  
					return (Object)Double.parseDouble(s);  
				} 
				else if (clazz == java.math.BigDecimal.class) {
					return (Object)java.math.BigDecimal.valueOf(Double.parseDouble(s));
				}
				else if (clazz == byte.class) {  
					return (Object)Byte.parseByte(s);  
				}  
				else if (clazz == float.class) {  
					return (Object)Float.parseFloat(s);  
				}  
				else if (clazz == short.class) {  
					return (Object)Short.parseShort(s);  
				}  
				else if (clazz == boolean.class) {  
					return (Object)Boolean.parseBoolean(s);  
				}  else if (clazz == String.class) {  
					return (Object)s;
				} else if (clazz == Date.class) {  
					/*
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					if(s.length()<11)return (Object)sdf1.parse(s);
					else return (Object)sdf.parse(s);
					*/
					return (Object)Utility.toDate(s);
					//  newMap.put(key, sdf.format(date));  
				} else if (clazz == Timestamp.class) {  
					/*
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					if(s.length()<11)return (Object)sdf1.parse(s);
					else return (Object)sdf.parse(s);
					*/
					return (Object)Utility.toDate(s);
					// newMap.put(key, sdf.format(date));  
				} else if (clazz == java.sql.Time.class) {
					/*
					SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");  
					return (Object)sdf.parse(s);
					*/
					return (Object)Utility.toDate(s);
					// newMap.put(key, sdf.format(sqlDate));  
				} else {  
					//newMap.put(key, value);  
					return (Object)s;
				}
			}
		}
		catch(Exception e)
		{
			return null;
		}
	}
	// string 类型转化
	public static String VauleToStr(Object value)
	{
	//GetPublicMsg msg = new GetPublicMsg();
		if (value == null)return null;
		Class clazz = (Class)value.getClass();
		
		if (clazz == null)return null;
		try{
			if (clazz == int.class) {  
				return String.valueOf((Integer)value);  
			}  
			else if (clazz == long.class) {  
				return String.valueOf((Long)value);  
			}  
			else if (clazz == double.class) {  
				return String.valueOf((Double)value);  
			}  
			else if (clazz == java.math.BigDecimal.class) {  
				return Utility.getFormatedStr((java.math.BigDecimal)value,1);
			}  
			else if (clazz == byte.class) {  
				return String.valueOf((Byte)value);
			}  
			else if (clazz == float.class) {  
				return String.valueOf((Float)value);  
			}  
			else if (clazz == short.class) {  
				return String.valueOf((Short)value);  
			}  
			else if (clazz == boolean.class) {  
				return String.valueOf((Boolean)value);  
			}  else if (clazz == String.class) {  
				return String.valueOf(value);
			} else if (clazz == Date.class) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return sdf.format(value);
			} else if (clazz == Timestamp.class) {  
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return sdf.format(value);
			} else if (clazz == java.sql.Time.class) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
				return sdf.format(value);
			} else {  
				return String.valueOf(value);
			}
		}
		catch(Exception e)
		{
			return null;
		}
	}
	public static Map RequestToMap(Object obj, HttpServletRequest request)
    {
        Class clazz = obj.getClass();
        Map map = new HashMap();
        try
        {
	        if(clazz.getSimpleName().equals("Object")){  
	            return map;  
	        }  
	        if (clazz.getDeclaredFields() == null || clazz.getDeclaredFields().length <= 0) {  
	            throw new Exception("当前对象中没有任何属性值");  
	        }
			Enumeration fields=request.getParameterNames();
			while(fields.hasMoreElements()){
				String sname=(String)fields.nextElement();
				Field field_name=null;
				try{
					field_name=clazz.getDeclaredField(sname);
				}
				catch(Exception ex){
					continue;
				}
				if(field_name==null)continue;
				String tval=request.getParameter(sname);
				Object value=field_name.getType();
				map.put(sname,StrToVaule(value,tval));
				//System.out.println(sname+" value="+StrToVaule(value,tval)+" type="+value);
			}
        }
        catch(Exception e){
            System.out.println("出错:" + e);
        }
        return map;
    }
	public static String encodeSpecial(String srcStr)
	{
		if (srcStr == null)
			return "";
		else
			return srcStr.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'").replace("\r\n", "\\r\\n").replace("\n", "\\n").replace("\r", "\\r");
	}
}
